package com.ych.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ych.beans.Message;
import com.ych.beans.Product;
import com.ych.beans.Sensor;
import com.ych.dao.ProductDao;
import com.ych.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao dao;
    @Override
    public Message addProduct(Product product) {
        Message message = new Message();
        if (product.getProductDate() == null){
            product.setProductDate(new Date(System.currentTimeMillis()));
        }
        if (product.getManuDate() == null){
            product.setManuDate(new Date(System.currentTimeMillis()));
        }
        boolean isSuccess = dao.addProduct(product);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("添加成功");
        }else {
            message.setMsg("添加失败");
        }
        return message;
    }

    @Override
    public Message deleteProduct(Integer id) {
        Message message = new Message();
        boolean isSuccess = dao.deleteProduct(id);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("删除成功");
        }else {
            message.setMsg("删除失败");
        }
        return message;
    }

    @Override
    public Message updateProduct(Product product) {
        Message message = new Message();
        boolean isSuccess = dao.updateProduct(product);
        message.setSuccess(isSuccess);
        if (isSuccess){
            message.setMsg("修改成功");
        }else {
            message.setMsg("修改失败");
        }
        return message;
    }

    @Override
    public Message selectProduct(Integer pageNo, String key) {
        Message message = new Message();
        if(pageNo!=null){
            PageHelper.startPage(pageNo, 8);
        }
        List<Product> list=dao.selectProduct(key);
        final PageInfo<Product> objectPageInfo = new PageInfo<>(list);
        message.setSuccess(true);
        if(!list.isEmpty()){
            message.setObj(objectPageInfo);
        }
        return message;
    }
}
